﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DataLib.Accounts.Bus;

namespace Ewts.Storage
{
    public partial class BillsByID : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["UserInfo"] != null)
                {
                    User curUser = new DataLib.Accounts.Bus.User();
                    curUser = (User)Session["UserInfo"];
                    if (Request["LoadNumid"] != null && Request["LoadNumid"].ToString().Trim() != "")
                        bindData(Request["LoadNumid"].ToString().Trim());
                }
                else
                {
                    Response.Redirect("/accounts/login.aspx");
                }
            }
        }
        int id = 0;
        private void bindData(string loadnumid)
        {
            DataTable dt = new DataLib.BLL.BillsConfirm().GetList("LoadNumID =" + Convert.ToInt32(loadnumid)).Tables[0];
            if (dt.Rows.Count != 0 && dt != null)
            {
                id = Convert.ToInt32(dt.Rows[0][0]);
                tdLoadnum.InnerText = dt.Rows[0]["LoadNumNo"].ToString();
                txtCEDate.Text = dt.Rows[0]["CEDate"].ToString();
                txtCAOpenBillDate.Text = dt.Rows[0]["CADateInvice"].ToString();
                txtCAPaymentDate.Text = dt.Rows[0]["CADatePay"].ToString();

                if (dt.Rows[0]["isCJO"].ToString() == "1")
                    this.cbCJO.Checked = true;
                else
                    this.cbCJO.Checked = false;
                if (dt.Rows[0]["isWRL"].ToString() == "1")
                    this.cbWRL.Checked = true;
                else
                    this.cbWRL.Checked = false;
                if (dt.Rows[0]["isMSF"].ToString() == "1")
                    this.cbMSF.Checked = true;
                else
                    this.cbMSF.Checked = false;
                if (dt.Rows[0]["isTran"].ToString() == "1")
                    this.cbTransferBill.Checked = true;
                else
                    this.cbTransferBill.Checked = false;


            }

            DataLib.Model.LoadNum loads = new DataLib.BLL.LoadNum().GetModel(Convert.ToInt32(loadnumid));

            DataLib.Model.Cumtomer cum = new DataLib.BLL.Cumtomer().GetModel((int)loads.customerid);

            tdLoadnum.InnerText = loads.loadnum;
            tdGoodsWeight.InnerText = loads.totalWeight.ToString();
            tdGoodsDescribe.InnerText = loads.goodsInfo;
            tdDestineCollDate.InnerText = loads.collectdate.ToString();

            tdCumCode.InnerText = cum.customercode;
            tdCumName.InnerText = cum.companyname;
            tdsalesman.InnerText = new DataLib.BLL.LoadNum().SalemanByLoad((int)loads.id);
            DataTable da=new DataLib.BLL.BarCode().GetList("loadnumid =" + loadnumid).Tables[0];
            if (da.Rows.Count!=0&&da!=null)
            {
                tdWRLDate.InnerText = da.Rows[0]["createdate"].ToString();
            }
            
           

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.txtCEDate.Text=="" || this.txtCAOpenBillDate.Text==""||this.txtCAPaymentDate.Text=="")
            {
                DataLib.Common.MessageBox.Show(Page, "请先选择日期！");
                return;
            }

            DataLib.Model.BillsConfirm bill = new DataLib.Model.BillsConfirm();

            DataTable dt = new DataLib.BLL.BillsConfirm().GetList("LoadNumID =" +  Convert.ToInt32(Request["LoadNumid"])).Tables[0];
            if (dt.Rows.Count == 0 || dt == null)
            {

                bill.LoadNumID = Convert.ToInt32(Request["LoadNumid"]);
                bill.LoadNumNo = Convert.ToInt32(tdLoadnum.InnerText);
                bill.CEDate = this.txtCEDate.Text ;
                bill.CADatePay = this.txtCAPaymentDate.Text;
                bill.CADateInvice = DateTime.Parse(this.txtCAOpenBillDate.Text);

                if (this.cbCJO.Checked)
                    bill.isCJO = 1;
                else
                    bill.isCJO = 0;

                if (this.cbWRL.Checked)
                    bill.isWRL = 1;
                else
                    bill.isWRL = 0;

                if (this.cbMSF.Checked)
                    bill.isMSF = 1;
                else
                    bill.isMSF = 0;

                if (this.cbTransferBill.Checked)
                    bill.isTran = 1;
                else
                    bill.isTran = 0;

                if (new DataLib.BLL.BillsConfirm().Add(bill) > 0)
                {
                    tdLoadnum.InnerText = "";
                    tdCumCode.InnerText = "";
                    tdCumName.InnerText = "";
                    tdsalesman.InnerText = "";
                    tdWRLDate.InnerText = "";
                    tdGoodsWeight.InnerText = "";
                    tdGoodsDescribe.InnerText = "";
                    tdDestineCollDate.InnerText = "";
                    txtCEDate.Text = "";
                    txtCAPaymentDate.Text = "";
                    txtCAOpenBillDate.Text = "";
                    cbCJO.Checked = false;
                    cbMSF.Checked = false;
                    cbTransferBill.Checked = false;
                    cbWRL.Checked = false;
                    DataLib.Common.MessageBox.ShowAndRedirect(Page, "保存成功", "BillsConfirmList.aspx");
                   
                }
            }
            else
            {
                DataLib.Model.BillsConfirm bl = new DataLib.BLL.BillsConfirm().GetModelList("LoadNumID =" + Convert.ToInt32(Request["LoadNumid"]))[0];
                bl.CEDate = this.txtCEDate.Text;
                bl.CADatePay = this.txtCAPaymentDate.Text;
                bl.CADateInvice = DateTime.Parse(this.txtCAOpenBillDate.Text);
                if (this.cbCJO.Checked)
                    bl.isCJO = 1;
                else
                    bl.isCJO = 0;

                if (this.cbWRL.Checked)
                    bl.isWRL = 1;
                else
                    bl.isWRL = 0;

                if (this.cbMSF.Checked)
                    bl.isMSF = 1;
                else
                    bl.isMSF = 0;

                if (this.cbTransferBill.Checked)
                    bl.isTran = 1;
                else
                    bl.isTran = 0;
                if (new DataLib.BLL.BillsConfirm().Update(bl))
                {
                    tdLoadnum.InnerText = "";
                    tdCumCode.InnerText = "";
                    tdCumName.InnerText = "";
                    tdsalesman.InnerText = "";
                    tdWRLDate.InnerText = "";
                    tdGoodsWeight.InnerText = "";
                    tdGoodsDescribe.InnerText = "";
                    tdDestineCollDate.InnerText = "";
                    txtCEDate.Text = "";
                    txtCAPaymentDate.Text = "";
                    txtCAOpenBillDate.Text = "";
                    cbCJO.Checked = false;
                    cbMSF.Checked = false;
                    cbTransferBill.Checked = false;
                    cbWRL.Checked = false;
                    DataLib.Common.MessageBox.ShowAndRedirect(Page, "保存成功", "BillsConfirmList.aspx");
                }
            }
        }
    }
}